import 'package:flutter/material.dart';

class KeepAliveWrapper extends StatefulWidget {

  const KeepAliveWrapper({Key? key, this.keepAlive = true, required this.child}) : super(key:key);

  final bool keepAlive;
  final Widget child;

  @override
  State createState() => KeepAliveWrapperState();
}

class KeepAliveWrapperState extends State<KeepAliveWrapper> with AutomaticKeepAliveClientMixin {

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return widget.child;
  }

  @override
  void didUpdateWidget(covariant KeepAliveWrapper oldWidget) {
    if (oldWidget.keepAlive != widget.keepAlive) {
      //更新keepAlive状态
      updateKeepAlive();
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  bool get wantKeepAlive => widget.keepAlive;

}